package dao

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"mscoin-common/msdb"
	"mscoin-common/msdb/gorms"
	"ucenter/internal/model"
)

type MemberDao struct {
	conn *gorms.GormConn //来自于common模块的工具类
}

func (m MemberDao) FindByPhone(ctx context.Context, phone string) (mem *model.Member, err error) {
	session := m.conn.Session(ctx)
	err = session.Model(&model.Member{}).
		Where("mobile_phone=?", phone).Limit(1).
		Find(&mem).Error

	if errors.Is(err, gorm.ErrRecordNotFound) { //如果确实没有记录, 查询结果是ErrRecordNotFond错误类型, 特殊判断一下进行返回nil
		return nil, nil
	}

	return mem, err
}

func NewMemberDao(db *msdb.MsDB) *MemberDao {
	return &MemberDao{
		conn: gorms.New(db.Conn),
	}
}
